﻿namespace KinonekoSoftware.Studio.Pages.Shenanigans
{
    partial class ForumEditorViewModel
    {
        private Character _owner;
        private Character _poster;

        private async void DoAddCharacterCommand()
        {
            // if (CharacterDataSource.HasItemInGlobalScoped)
            // {
            //     
            // }

            //
            //
            var r = await CharacterPickerViewModel.Multiple(_characterDataSource.TransientScopedDictionary.OriginalHashSet);

            if (!r.IsFinished)
            {
                return;
            }

            var members = r.Value;

            //
            // 添加角色
            foreach (var member in members)
            {
                //
                //
                _characterDataSource.ScopedAdd(member);
                _discuss.Members.Add(member.Id);
            }

            RaiseUpdated(nameof(HasCharacter));
            RaiseUpdated(nameof(Characters));
        }

        private void DoUpdateCharacterCommand()
        {
            _characterDataSource.ScopedUpdate();
        }

        private async void DoRemoveCharacterCommand(Character character)
        {
            if (character is null)
            {
                return;
            }

            if (IsForumOwner(character))
            {
                await Danger(TitleSR.DangerOperation, "不允许删除版主！");
                return;
            }

            if (!await RemoveItem())
            {
                return;
            }

            _characterDataSource.ScopedRemove(character);
            _discuss.Members.Remove(character.Id);
        }

        private async void DoRemoveAllCharacterCommand()
        {

            if (!await RemoveItem(TitleSR.DangerOperation, TextSR.Remove_Item_All))
            {
                return;
            }

            foreach (var character in Characters)
            {
                if (IsForumOwner(character))
                {
                    continue;
                }

                _characterDataSource.ScopedRemove(character);
                _discuss.Members.Remove(character.Id);
            }
        }

        internal bool IsForumOwner(Character  character) => character is not null && _discuss is not null && _discuss.OwnerID == character.Id;
        internal int  GetPostAmount(Character character) => _ForumMessageCollection.Count(x => x.Target.PosterID == character.Id);

        /// <summary>
        /// 当前的发帖人。
        /// </summary>
        public Character Poster
        {
            get => _poster;
            set
            {
                SetValue(ref _poster, value);
                RaiseUpdated(nameof(PosterPostAmount));
            }
        }

        public int       PosterPostAmount => _poster is not null ? GetPostAmount(_poster) : 0;
        public Character Owner            => _owner ??= _characterDataSource.ScopedGet(_discuss.OwnerID);
        public bool      HasCharacter     => Characters.Count > 0;

        public ICommandEX AddCharacterCommand       { get; }
        public ICommandEX UpdateCharacterCommand    { get; }
        public ICommandEX RemoveCharacterCommand    { get; }
        public ICommandEX RemoveAllCharacterCommand { get; }

        public ObservableCollection<Character> Characters => _characterDataSource.TransientScopedCollection;
    }
}